<template>
  <div>
    <h3>用户管理-组合API</h3>
    <h4>用户总数: {{ userCount }}</h4>
    <hr />
    <table width="80%" border="1" align="center">
      <thead>
        <tr>
          <th>id</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in userList" :key="item.id" align="center">
          <td>{{ item.id }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
          <td>
            <button @click="del(item.id)">删除</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import useUserStore from "../store/userStore";
import { storeToRefs } from "pinia";
export default {
  setup() {
    // 创建数据存储对象
    const userStore = useUserStore();
    // 获取actions方法
    const { userAdd, userDel } = userStore;
    // 获取数据, 并使数据继续保持响应式特性
    const { userList, userCount } = storeToRefs(userStore);
    return {
      // userList:userStore.userList,
      userList,
      userCount,
      userAdd,
      userDel,
    };
  },
  methods: {
    del(id) {
      this.userDel(id);
    },
  },
};
</script>

<style>
</style>